package com.qqtech.qquan.hi.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.hi.enums.HiRuleGroupTypeDictEnum;
import com.qqtech.qquan.hi.model.HiRuleVo;
import com.qqtech.qquan.hi.model.HiUserTask;
import com.qqtech.qquan.hi.model.HiUserTaskQuery;
import com.qqtech.qquan.hi.model.HiUserTaskVo;
import com.qqtech.qquan.hi.service.HiRuleService;
import com.qqtech.qquan.hi.service.HiSysDictService;
import com.qqtech.qquan.hi.service.HiUserTaskService;
import com.qqtech.qquan.user.model.UserLoginVo;
import com.qqtech.qquan.user.model.UserValueVo;
import com.qqtech.qquan.user.service.UserValueService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/hi/user/task")
public class HiUserTaskController extends BaseWebController<HiUserTask, HiUserTaskQuery> {
	@Resource
	private HiUserTaskService hiUserTaskService;

	@Resource
	private HiRuleService hiRuleService;
	@Resource
	private HiSysDictService hiSysDictService;

	@Resource
	private UserValueService userValueService;

	@Override
	protected BaseService<HiUserTask> getBaseService() {
		return hiUserTaskService;
	}

	/**
	 * 我的任务
	 *
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myTask")
	@ResponseBody
	public Result myTask() {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		HiUserTaskQuery taskQ = new HiUserTaskQuery();
		taskQ.setUserId(curUserId);
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "groupType"),
				new PageOrder(Direction.ASC, "id"));
		Page<HiUserTaskVo> taskPage = hiUserTaskService.queryPageList(taskQ, pageKit);
		List<HiUserTaskVo> taskList = taskPage.getContent();
		LinkedHashMap<String, List<HiUserTaskVo>> map = new LinkedHashMap<String, List<HiUserTaskVo>>();
		int finishQuanValue = 0;
		int finishHonorValue = 0;
		int totalQuanValue = 0;// 目前总共完成多少圈币
		int totalHonorValue = 0;// 目前总共完成多少积分
		String finishValueString = "";
		if (taskList != null && taskList.size() > 0) {
			finishQuanValue = taskList.get(0).getFinishQuanValue();
			finishHonorValue = taskList.get(0).getFinishHonorValue();
			HiRuleVo ruleVo = null;
			for (HiUserTaskVo task : taskList) {
				ruleVo = hiRuleService.getById(task.getRuleId());
				if (ruleVo != null) {
					task.setQuanValue(ruleVo.getQuanValue());
					task.setHonorValue(ruleVo.getHonorValue());
				}
				if (null == map.get(String.valueOf(task.getGroupType()))) {
					List<HiUserTaskVo> aList = new ArrayList<HiUserTaskVo>();
					aList.add(task);
					map.put(String.valueOf(task.getGroupType()), aList);
				} else {
					List<HiUserTaskVo> aList = (List<HiUserTaskVo>) map.get(String.valueOf(task.getGroupType()));
					aList.add(task);
				}
			}
		}

		String honorLevelName = "";
		UserValueVo userValue = userValueService.getById(curUserId);
		if (userValue != null) {
			honorLevelName = userValue.getHonorLevelName();
			totalQuanValue = userValue.getQuanValue();
			totalHonorValue = userValue.getHonorValue();
		}
		UserLoginVo userLoginVo = super.getLoginUser();
		result.put("userLoginVo", userLoginVo);
		finishValueString = finishHonorValue + "荣誉值+" + finishQuanValue + "圈币";
		result.put("finishValueString", finishValueString);
		result.put("totalQuanValue", totalQuanValue);
		result.put("totalHonorValue", totalHonorValue);
		result.put("honorLevelName", honorLevelName);

		result.put("taskGroup", HiRuleGroupTypeDictEnum.getList());
		result.put("taskMap", map);
		return result;
	}

}
